package utils;

import models.EstateQuery;



public class mySqlUtil {
	public static String getEstateQueryString(EstateQuery estateQuery,String z_id) {
		String cond = "1=1";
		if (estateQuery == null) return cond;
		if (!estateQuery.getLocal_id().equals(""))
			cond += " and local_id = '"+estateQuery.getLocal_id()+"'";
		
		if (!estateQuery.getSelfcode().equals(""))
			cond += " and selfcode = '"+estateQuery.getSelfcode()+"'";
		
		if (!estateQuery.getMode().equals(""))
			cond += " and mode = '"+estateQuery.getMode()+"'";
		
		if (!estateQuery.getDatetype().equals("0")){
			if (!estateQuery.getDateselect().equals(""))				
				cond += " and editdate >= '"+  DateUtil.formatter(DateUtil.getDayOutDate(DateUtil.getNowDate(), Integer.parseInt(estateQuery.getDateselect())).getTime())+"'";
		}else{
			if (!estateQuery.getEditdate1().equals(""))
				cond += " and editdate >= '"+estateQuery.getEditdate1()+"'";
			if (!estateQuery.getEditdate2().equals(""))
				cond += " and editdate <= '"+estateQuery.getEditdate2()+"'";
		}
		
		if ((!estateQuery.getPurpose1().equals("")) || (!estateQuery.getPurpose2().equals("")))
			cond += " and ( purpose ='"+estateQuery.getPurpose1()+"' or purpose = '"+ estateQuery.getPurpose2() + "' )";
		
		if ((!estateQuery.getRoom1().equals("")) || (!estateQuery.getRoom2().equals("")) || (!estateQuery.getRoom3().equals("")))
			cond += " and ( room like '%"+estateQuery.getRoom1()+"%' or room like '%"+ estateQuery.getRoom2() + "%' or room like '%"+ estateQuery.getRoom3() + "%')";
		
		if (!estateQuery.getMap().equals(""))
			cond += " map in ("+estateQuery.getMap()+")";
		
		
		if ((!estateQuery.getAddress1().equals("")) || (!estateQuery.getAddress2().equals("")) || (!estateQuery.getAddress3().equals("")))
			cond += " and ( address like '%"+estateQuery.getAddress1()+"%' or address2 like '%"+ estateQuery.getAddress2() + "%' or address like '%"+ estateQuery.getAddress3() + "%')";
			
		if ((!estateQuery.getLayer1().equals("")) || (!estateQuery.getLayer2().equals("")))
		{
			String layer1 = estateQuery.getLayer1();
			String layer2 = estateQuery.getLayer2();
			if (layer1.equals("")) layer1 = "1";
			if (layer2.equals("")) layer1 = "99";
			cond += " and layer >= '"+layer1 +"' and layer <= '"+layer2+"'";  //这里需要优化，可能不是数字					
		}
		
		if ((!estateQuery.getArea1().equals("")) || (!estateQuery.getArea2().equals("")))
		{
			String area1 = estateQuery.getArea1();
			String area2 = estateQuery.getArea2();
			if (area1.equals("")) area1 = "0";
			if (area2.equals("")) area2 = "999999";
			cond += " and area >= "+area1 +" and area <= "+area2;
		}
		
		if ((!estateQuery.getPrice1().equals("")) || (!estateQuery.getPrice2().equals("")))
		{
			String price1 = estateQuery.getPrice1();
			String price2 = estateQuery.getPrice2();
			if (price1.equals("")) price1 = "0";
			if (price2.equals("")) price2 = "9999999";
			cond += " and price >= "+price1 +" and price <= "+price2;
		}
		
		if ((!estateQuery.getZrf1().equals("")) || (!estateQuery.getZrf2().equals("")))
		{
			String zrf1 = estateQuery.getZrf1();
			String zrf2 = estateQuery.getZrf2();
			if (zrf1.equals("")) zrf1 = "0";
			if (zrf2.equals("")) zrf2 = "9999999";
			cond += " and fzrf >= "+zrf1 +" and fzrf <= "+zrf2;
		}
		
		if (!estateQuery.getUnit().equals(""))
			cond += " and unit = '"+estateQuery.getUnit()+"'";
		
		if (!estateQuery.getFitment().equals("")){
			String fitment = estateQuery.getFitment();
			if (fitment.equals("无")) 
               cond = " and fitment = '白坯'";
			else               
			    cond += " and fitment <> '' and fitment <> '白坯'";
		}
		
		if (!estateQuery.getScheduled_flag().equals("")){
			String scheduled_flag = estateQuery.getScheduled_flag();
			if (scheduled_flag.equals("无")) scheduled_flag = ""; 
			cond += " and scheduled_flag = '" +scheduled_flag+ "'";
		}
		if (!estateQuery.getUnitname().equals(""))
			cond += " and unitname like '%" +estateQuery.getUnitname()+ "%'";
		
		if  ((estateQuery.getShowunit()!=null) && (estateQuery.getShowunit().equals("showunit")))
			cond += " and z_id = '"+z_id+"'";
			
		if ((!estateQuery.getCircs1().equals("")) || (!estateQuery.getCircs2().equals("")))
			cond += " and ( circs like '%"+estateQuery.getCircs1()+"%' or circs like '%"+ estateQuery.getCircs2() + "%' )";
		
		if ((!estateQuery.getFitment1().equals("")) || (!estateQuery.getFitment2().equals("")))
			cond += " and ( fitment like '%"+estateQuery.getFitment1()+"%' or fitment like '%"+ estateQuery.getFitment2() + "%' )";
		
		if (!estateQuery.getDirection().equals(""))
			cond += " and direction = '"+estateQuery.getDirection()+"'";
		
		if (!estateQuery.getMemo().equals(""))
			cond += " and memo like '%"+estateQuery.getMemo()+"%'";
		
		if ((!estateQuery.getQt1().equals("")) || (!estateQuery.getQt2().equals("")) || (!estateQuery.getQt3().equals("")))
			cond += " and ( qt1 like '%"+estateQuery.getQt1()+"%' or qt2 like '%"+ estateQuery.getQt2() + "%' or qt3 like '%"+ estateQuery.getQt3() + "%')";
		
		if (!estateQuery.getDisplay().equals(""))
			cond += " and display like '%"+estateQuery.getDisplay()+"%'";
		
		if (!estateQuery.getWg().equals(""))
			cond += " and wg like '%"+estateQuery.getWg()+"%'";
		
		if (!estateQuery.getPhone_flag().equals("")){
			String phone_flag = estateQuery.getPhone_flag();
			if (phone_flag.equals("无")) phone_flag = ""; 
			cond += " and fitment = '" +phone_flag+ "'";
		}
		return cond;
	}
}
